/*
 *   Copyright 2021 zzh
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 */

package com.aduib.boot.common.serialization.kryo.optimization;

import com.aduib.boot.common.serialization.api.ObjectWriter;
import com.aduib.boot.common.serialization.api.SerializationClean;
import com.aduib.boot.common.serialization.kryo.utils.KryoUtils;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Output;

import java.io.IOException;
import java.io.OutputStream;

/**
 * @description: KryoObjectWriter
 * @author: zzh
 * @date: 2021/10/12 10:48
 */
public class KryoObjectWriter implements ObjectWriter, SerializationClean {

    private Kryo kryo;

    private Output output;

    public KryoObjectWriter(OutputStream outputStream) {
        this.output=KryoUtils.getOutput();
        output.setOutputStream(outputStream);
        this.kryo = KryoUtils.get();
    }

    @Override
    public void cleanup() {
        KryoUtils.releaseOutput(output);
        KryoUtils.release(kryo);
    }

    @Override
    public void writeObject(Object v) throws IOException {
        try {
            kryo.writeObjectOrNull(output,v,v.getClass());
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeBoolean(boolean v) throws IOException {
        try {
            output.writeBoolean(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeByte(byte v) throws IOException {
        try {
            output.writeByte(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeBytes(byte[] v) throws IOException {
        try {
            if (v == null) {
                output.writeInt(-1);
            } else {
                output.writeBytes(v);
            }
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeBytes(byte[] v, int off, int len) throws IOException {
        try {
            if (v == null) {
                output.writeInt(-1);
            } else {
                output.writeInt(len);
                output.writeBytes(v, off, len);
            }
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeShort(short v) throws IOException {
        try {
            output.writeShort(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeInt(int v) throws IOException {
        try {
            output.writeInt(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeLong(long v) throws IOException {
        try {
            output.writeLong(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeFloat(float v) throws IOException {
        try {
            output.writeFloat(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeDouble(double v) throws IOException {
        try {
            output.writeDouble(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void writeUtfString(String v) throws IOException {
        try {
            output.writeString(v);
        }catch(KryoException e){
            throw new IOException(e);
        }
    }

    @Override
    public void flushBuffer() throws IOException {
        output.flush();
    }
}
